/*
 * @Descripttion: redux-saga的入口文件，里面有createSagaMiddleware
 * @Author: lukasavage
 * @Date: 2021-11-30 20:47:07
 * @LastEditors: lukasavage
 * @LastEditTime: 2021-11-30 21:16:40
 */
import EventEmitter from 'events';

import runSaga from './runSaga'

export default function createSagaMiddleware() {
    let eventEmitter = new EventEmitter();
    let boundRunSaga;
    function sagaMiddleware({getState,dispatch}) {
        boundRunSaga=runSaga.bind(null,{eventEmitter,dispatch,getState});
        return function (next) {
            return function (action) {
                const result = next(action);
                eventEmitter.emit(action.type, action);
                return result;
            }
        }
    }
    sagaMiddleware.run = (saga)=>boundRunSaga(saga);
    return sagaMiddleware;
}
